package it.polimi.traveldream.sharedFunction;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import it.polimi.traveldream.managers.ProductManager;
import it.polimi.traveldream.persistence.Hotel;
import it.polimi.traveldream.persistence.Localita;
import it.polimi.traveldream.persistence.Utente;
import it.polimi.traveldream.persistence.dtos.HotelProdDTO;



public class ConvertClass {
	
	@PersistenceContext
	private EntityManager em;
	
	
	public HotelProdDTO convertiHotel(Hotel hotel){
		HotelProdDTO hotelDTO= new HotelProdDTO();
		hotelDTO.setID(hotel.getIDHotel());
		hotelDTO.setEmailImpiegato(hotel.getUtente().getEmail());
		hotelDTO.setLocalitàID(hotel.getLocalita().getIDLocalita());
		hotelDTO.setNome(hotel.getNome());
		hotelDTO.setNumeroCivico(hotel.getNumeroCivico());
		hotelDTO.setVia(hotel.getVia());
		return hotelDTO;
	}

	public Hotel convert(HotelProdDTO hotelDTO) {
		Hotel hotel = new Hotel();
		
		updateEntityBasicFields(hotelDTO, hotel);
		return hotel;
	}
	
	private void  updateEntityBasicFields(HotelProdDTO hotelDTO, Hotel hotel) {
		try{
		System.out.print("Converto in ent.");
		hotel.setLocalita(findLocalitaEntity(hotelDTO.getLocalitàID()));
		hotel.setNome(hotelDTO.getNome());
		hotel.setNumeroCivico(hotelDTO.getNumeroCivico());
		hotel.setVia(hotelDTO.getVia());
		hotel.setUtente(findUtenteEntity(hotelDTO.getEmailImpiegato()));
		//hotel.setUtente(getUserCredManager().findUser(hotelDTO.getEmailImpiegato()));
		System.out.print("Convertito in ent.");
		}catch(Exception e){}				
	}
	
	
	public Localita findLocalitaEntity(int localitàID) {
		return em.find(Localita.class, localitàID);
	}
	
	public Utente findUtenteEntity(String email) {
		return em.find(Utente.class, email);
	}


}
